/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logging;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;
import logging.LogLevel.Level;

/**
 *
 * @author Marco
 */
public class FileAppender extends IAppender {

    @Override
    public String toString() {
        return "FileLogLevel";
    }

    public void configure() {
    }

    public void write(Level level, String msg, Exception ex) {

        try {
            PrintStream logFile = new PrintStream(
                    new FileOutputStream("log.txt", true));

            try {
                logFile.println(Logger.getDateFormat().format(new Date()) + " " + level.toString() + " " + msg + " " + ex.getMessage());
            } finally {
                logFile.close();
            }
        } catch (IOException exc) {
            exc.printStackTrace();
        }

    }

    @Override
    public void write(Level level, String msg) {
        try {
            PrintStream logFile = new PrintStream(
                    new FileOutputStream("log.txt", true));

            try {
                logFile.println(Logger.getDateFormat().format(new Date()) + " " + level.toString() + " "+ (msg.equals("") ? "OK" : msg));
            } finally {
                logFile.close();
            }
        } catch (IOException exc) {
            exc.printStackTrace();
        }
    }
}
